package server;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class StaticResourceUtil {
    /**
     * 获取静态资源的绝对路径
     * @param path
     * @return
     */
    public static String getAbsolutePath(String path){
        //classes目录绝对路径
        String absolutePath=
                StaticResourceUtil.class.getResource("/").getPath();
        return absolutePath.replace("\\\\","/")+path;
    }
    /**
     * 读取静态资源输入流，通过输出流输出
     */
    public static void outputStaticResource(InputStream inputStream, OutputStream outputStream) throws IOException {

        int count = 0;
        while (count == 0) {
            count = inputStream.available();
        }
        int resourceSize=count;
       // byte[] bytes = new byte[count];
       // inputStream.read(bytes);
       outputStream.write(HttpProtocolUtil.getHttpHeader200(resourceSize).getBytes());

        long written=0;//已经读取的内容长度
        int byteSize=1024;//计划每次缓冲长度
        byte[] bytes = new byte[byteSize];
        while (written<resourceSize){
            if(written+byteSize>resourceSize){
                byteSize=(int)(resourceSize-written);//剩余的文件长度
                bytes=new byte[byteSize];
            }
            inputStream.read(bytes);
            outputStream.write(bytes);

            outputStream.flush();
            written+=byteSize;
        }

    }



}
